<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019\3\7 0007
 * Time: 14:26
 */

namespace app\jiangnan\controller;
use think\Db;
use think\Controller;
use think\facade\Log;
use app\service\jiangnanService;
class Applet extends Controller
{
   
    public function ewm(){
        $url_param="pages/index/index";
        $query_param="x=1";
        $describe="This is a applet QR code";
        $api = new jiangnanService();
        $result=$api->qrcodeCreate(json_encode(['url_param'=> $url_param,'query_param'=>$query_param,'describe'=>$describe]));
        var_dump($result);

    }
    
    public function formIdAdd(){
        $applet_form["ctime"]=time();
        $applet_form["user_id"]=request()->get("user_id");
        $applet_form["form_id"]=request()->get("formId");
        $applet_form["num"]=3;
        $res=Db::table("applet_form")->insert($applet_form);
    }
	 //获取支付宝用户的user_id
    public function userId()
    {
        $api = new jiangnanService();
        //获取当前车辆支付宝ID
        $code = request()->get('auth_code');
        $oauthtoken = $api->xcxoauthToken($code);
        Log::write($oauthtoken,"info",true);
        if(property_exists($oauthtoken,'sub_msg')){
            return json(['info' => $oauthtoken->sub_msg]);
        }
        $user_id = $oauthtoken->user_id;//user_id
        return json(['info' => $user_id]);
        return json_encode($user_id);
    }
	
    //业主获取短信验证码
    public function verifyroom()
    {
        $data=request()->post();
        //查询该用户是否绑定房屋

        //查询该手机号下是否存在房屋
        $roominfo=Db::table("roominfo")->where("phone",$data["phone"])->find();
        if(empty($roominfo))
        {
            return json(["errCode"=>"1",'error' => '亲爱的业主：您的手机号码验证错误，可能是预留在物业服务中心手机号码没有及时更新，为更快的解决您的问题，您可以在线客服留言或致电我们：，感谢您的理解支持。']);
        }
        //查询业主是否已经绑定该房屋
        $userroom=Db::table("roominfo")
                    ->where("id",$roominfo["id"])
                    ->where("user_id",'neq','0')
                    ->find();
        if(!empty($userroom))
        {
            return json(["errCode"=>"1",'error' => '该房屋已经绑定']);
        }
        //$arr=array("errCode"=>"0","msg"=>"验证码发送成功");
        //return json_encode($arr);
        $result=$this->sendmsg($data["phone"]);
        return $result;
    }

    //短信验证码
    public function sendmsg($phone)
    {
        $code=rand(100000,999999);
        //$phone=request()->param("phone");
        $send = send_sms($phone, 1, ['code' => $code]);
        if($send['Message'] == 'OK') 
        {
            $info["phone"]=$phone;
            $info["code"]=$code;
            $info["ctime"]=time();
            $result=Db::table("sms_verify")->insert($info);
            if($result)
            {
                return json(["errCode"=>"0",'error' => '验证码发送成功']);
                $arr=array("errCode"=>"0","msg"=>"验证码发送成功");
                return json_encode($arr);
                //die;
            }else
            {
                return json(["errCode"=>"1",'error' => '请重新获取验证码']);
                $arr=array("errCode"=>"1","msg"=>"请重新获取验证码");
                return json_encode($arr);
                //die;
            }
        } else 
        {
            return json(["errCode"=>"1",'error' => $send['Message']]);
            $arr=array("errCode"=>"1","msg"=>$send['Message']);
            return json_encode($arr);
            //die;
        }
    }
    
    //业主绑定房屋
    public function bindroom()
    {
        $data=request()->post();
        //手机号验证
        $smsverify=Db::table("sms_verify")
                ->where("phone",$data["phone"])
                ->where("code",$data["code"])
                ->where("ctime",'>',(time()-80))
                ->order("id desc")
                ->find();
        if(empty($smsverify))
        {
            return json(["errCode"=>"1",'error' => '验证码错误']);
        }else
        {
            $info["status"]="2";
            Db::table("sms_verify")
                    ->where("id",$smsverify["id"])
                    ->update($info);
            //执行绑定房屋操作
            $roominfo=Db::table("roominfo")
                        ->where("phone",$data["phone"])
                        ->find();
            if(empty($roominfo))
            {
                return json(["errCode"=>"1",'error' => '房屋绑定失败，请重新获取验证码绑定1']);
            }

            //Log::write($data,"yang");
            $res=Db::table("roominfo")->where("id",$roominfo["id"])->update(["user_id"=>$data["user_id"],"update_time"=>date('Y-m-d H:i:s')]);
            if($res)
            {
                return json(["errCode"=>"0",'error' => '房屋绑定成功']);
            }else
            {
                return json(["errCode"=>"1",'error' => '房屋绑定失败，请重新获取验证码绑定3']);
            }
        }
    }

    //查询用户绑定状态
    public function userinquire()
    {
        $user_id=request()->post("user_id");
        if(strlen($user_id)!='16'){
            return json(["bindstatus"=>"2"]);
        }
        $userinfo=Db::table("roominfo")
                    ->where("user_id",$user_id)
                    ->find();
        if(empty($userinfo))
        {
            return json(["bindstatus"=>"0"]);//未绑定
        }else{
            return json(["bindstatus"=>"1"]);//已绑定
        }
    }
    //业主验证
    public function verifyOwner()
    {
        $user_id=request()->post("user_id");
         if(strlen($user_id)!='16')
        {
            return json(["errCode"=>"1",'errormsg' => '亲爱的业主，如果您需要修改信息，请联系您的户主或管理员，可在线客服提供帮助或致电我们0']);
        }
        //查询是否是业主
        $roominfo=Db::table("roominfo")
                            ->where("user_id",$user_id)
                           
                            ->find();
        if(empty($roominfo))
        {
            return json(["errCode"=>"1",'errormsg' => '亲爱的业主，如果您需要修改信息，请联系您的户主或管理员，可在线客服提供帮助']);
        }else
        {
            return json(["errCode"=>"0",'msg' => '成功']);
        }   
    }

    public function contract(){
        return json(["errCode"=>"0",'Msg' => "操作成功","phone"=>"82757273"]);
    }

	//将对象转化成数组
	//调用这个函数，将其幻化为数组，然后取出对应值
	public function object_array($array)
	{
	   if(is_object($array))
	   {
		  $array = (array)$array;
	   }
	   if(is_array($array))
	   {
		  foreach($array as $key=>$value)
		  {
		      $array[$key] = object_array($value);
		  }
	   }
	   return $array;
	}
    
}